import {ModalForm, ProFormSelect, ProFormText} from "@ant-design/pro-components";
import {useRef} from "react";
import {ProFormInstance} from "@ant-design/pro-form/lib";
import {RequestOptionsType} from "@ant-design/pro-utils";
import {useModel, useSnapshot} from "@@/exports";
import {NotifyModel} from "@/models/notify";


export interface NotifyUpdateModalProps {
    onFinish: (formData: any) => Promise<boolean | void>;
}

const NotifyUpdateModal = (props: NotifyUpdateModalProps) => {
    let {getDictList} = useModel("dict");
    let notifyState = useSnapshot(NotifyModel.state);
    let {actions: notifyActions} = NotifyModel

    const formRef = useRef<ProFormInstance>()
    return <>
        <ModalForm
            key={notifyState.notify?.id || -1}
            formRef={formRef}
            title={notifyState.notify ? "修改通知" : "添加通知"}
            open={notifyState.updateModalState}
            initialValues={notifyState.notify}
            onFinish={props.onFinish}
            onOpenChange={notifyActions.setUpdateModalState}
        >
            <ProFormText
                label="id"
                hidden
                name="id"
            />
            <ProFormSelect
                label="通知类型"
                name="notifyType"
                request={async () => getDictList('notify_type') as RequestOptionsType[]}
                rules={[{required: true, message: "请选择通知类型"}]}
            />
            <ProFormText
                label="webhook地址"
                name="webhookUrl"
                rules={[{required: true, message: "请填写webhook地址"}]}
            />
            <ProFormText
                label="备注"
                name="remark"
                rules={[{required: true, message: "请填写备注"}]}
            />
        </ModalForm>
    </>
}

export default NotifyUpdateModal
